import requests
import json
from setenv import setLotteryDir
from setenv import getFileDir
from setenv import getFileName
from read_mysql_lottery import get_num_periods
from read_mysql_lottery import get_lottery_num_mysql
from read_mysql_lottery import save_mysql_lottery



def fetch_dltlotto_numbers_mysql(url,num_periods):
    # 创建数据库连接

    lotteryDrawResult = []

    response = requests.get(url)
    setLotteryDir()

    if response.status_code == 200:
        html = response.text
        # print(html)
        # 假设中奖号码在HTML中的某个特定标签内，例如：<div class="lotto-numbers">
        data = json.loads(str(html))
        # print(data)
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawNum'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawResult'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryDrawTime'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryGameName'))
        # print(data.get('value').get('lastPoolDraw').get('lotteryGameNum'))
        # lotteryDrawNum = data.get('value').get('lastPoolDraw').get('lotteryDrawNum')
        # lotteryDrawResult = data.get('value').get('lastPoolDraw').get('lotteryDrawResult')
        # lotteryDrawTime = data.get('value').get('lastPoolDraw').get('lotteryDrawTime')
        # lotteryGameName = data.get('value').get('lastPoolDraw').get('lotteryGameName')
        # lotteryGameNum = data.get('value').get('lastPoolDraw').get('lotteryGameNum')
        for i in range(0,len(data.get('value').get('list'))):
            # print(data.get('value').get('list')[i].get('lotteryDrawNum'))
            # print(data.get('value').get('list')[i].get('lotteryDrawResult'))
            # print(data.get('value').get('list')[i].get('lotteryDrawTime'))
            # print(data.get('value').get('list')[i].get('lotteryGameName'))
            # print(data.get('value').get('list')[i].get('lotteryGameNum'))
            lotteryDrawNum = data.get('value').get('list')[i].get('lotteryDrawNum')
            lotteryDrawResult = data.get('value').get('list')[i].get('lotteryDrawResult').split(' ')
            lotteryDrawTime = data.get('value').get('list')[i].get('lotteryDrawTime')
            lotteryGameName = data.get('value').get('list')[i].get('lotteryGameName')
            lotteryGameNum = data.get('value').get('list')[i].get('lotteryGameNum')
            if lotteryDrawNum == num_periods:
                sql = "INSERT INTO lottery_dlt (record_date,num_periods,num1,num2,num3,num4,num5,num6,num7) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
                values = (lotteryDrawTime, lotteryDrawNum, lotteryDrawResult[0], lotteryDrawResult[1], lotteryDrawResult[2], lotteryDrawResult[3], lotteryDrawResult[4], lotteryDrawResult[5], lotteryDrawResult[6])
                save_mysql_lottery(sql,values)
                return lotteryDrawResult

    return []





if __name__ == '__main__':
    url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=1&isVerify=0&pageNo=1'  # 替换为实际的大乐透中奖号码公布页面URL

    num_periods = get_num_periods("dlt",1)
    print(num_periods)
    table_name = "lottery_dlt"
    lottery_num = get_lottery_num_mysql(table_name,num_periods)
    print(lottery_num)
    if len(lottery_num) == 0:
        print("mysql无数据")
        numbers = fetch_lotto_numbers_mysql(url,num_periods)
        if len(numbers) == 0:
            print("num_periods未开奖")
        print(numbers)
    # numbers = fetch_lotto_numbers_mysql(url)
    # if numbers:
    #     print("大乐透中奖号码：", numbers)
    # else:
    #     print("未能抓取到中奖号码。")